package com.tanhua.server.controller;

import com.tanhua.dubbo.server.vo.UsersCountVo;
import com.tanhua.server.service.UsersCountService;
import com.tanhua.server.vo.ActiveUsersVo;
import com.tanhua.server.vo.DistributionVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("/management/dashboard")
@Slf4j
public class UsersCountController {

    @Autowired
    private UsersCountService usersCountService;

    @GetMapping("/summary")
    public ResponseEntity<UsersCountVo> queryInfo(){
        try {
            UsersCountVo usersCountVo = usersCountService.queryInfo();
            if (usersCountVo != null)
                return ResponseEntity.ok(usersCountVo);
        } catch (Exception e) {
            log.error("查询出错,请检查",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }



/*
* 101: 新增  102: 活跃 103: 次日留存率
* */
    @GetMapping("/users")
    public ResponseEntity<ActiveUsersVo> countUsers(@RequestHeader("Authorization")String tooken
                                            , String sd, String ed, String type){
        try {
            ActiveUsersVo result = usersCountService.countUsers(Long.valueOf(sd),Long.valueOf(ed),type);
            if (result != null)
                return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("查询出错,请检查",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("/distribution")
    public ResponseEntity<DistributionVo> userDistribution(@RequestHeader("Authorization")String tooken
            , String sd, String ed){
        try {
            DistributionVo distributionVo = usersCountService.userDistribution(Long.valueOf(sd), Long.valueOf(ed));
            if (distributionVo != null)
                return ResponseEntity.ok(distributionVo);
        } catch (Exception e) {
            log.error("统计出错,请检查",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
